﻿package {
	
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.geom.Point;
import flash.geom.Rectangle;



public class Pixel extends Shape {
	
	// Properties
	// -----------------------------------------------------------
	
	private var red:Rectangle;
	private var green:Rectangle;
	private var blue:Rectangle;
	
	private var _color:uint;
	private var _origin:Point;
	
	private var _screen:BitmapData;
	private var _pixelSize:int;
	private var _gateSize:int;
	private var _phosphorWidth:int;
	private var _phosphorHeight:int;
	
	
	
	// Constructor
	// -----------------------------------------------------------
	
	public function Pixel($screen:BitmapData,
				  $pixelSize:int = 3,
				  $gateSize:int = 0) {
		
		_screen		= $screen;
		_pixelSize		= $pixelSize;
		_gateSize		= $gateSize;
		_phosphorWidth	= _pixelSize / 3;
		_phosphorHeight	= _pixelSize;
		
		if (_gateSize > 0) {
			_phosphorHeight	= _pixelSize - _gateSize;
			_phosphorWidth	= _phosphorWidth - _gateSize;
		}
		
		build();
	}
	
	
	
	// Public Functions
	// -----------------------------------------------------------
	
	public function set color($color:uint):void {
		
		var a:uint = ($color >> 24) << 24;
		
		if (a > 0) {
			var r:uint 		= (($color >> 16) << 16) | a;
			var g:uint		= (($color >> 8 & 0xff) << 8) | a;
			var b:uint		= ($color & 0xff) | a;
			
			// Change Phosphor Colors
			_screen.fillRect	(red, r);
			_screen.fillRect	(green, g);
			_screen.fillRect	(blue, b);
			
			// Change Pixel Color
			opaqueBackground	= $color;
		}
		
		_color	= $color;
	}
	
	
	public function set origin($origin:Point):void {
		
		x 		= $origin.x;
		y 		= $origin.y;
		
		red.x		+= $origin.x;
		red.y		= $origin.y;
		green.x	+= $origin.x;
		green.y	= $origin.y;
		blue.x		+= $origin.x;
		blue.y		= $origin.y;
		
		_origin	= $origin;
	}
	
	
	
	// Private Functions
	// -----------------------------------------------------------
	
	private function build():void {
		
		var pos1:Number	= (_pixelSize * 0/3);
		var pos2:Number	= (_pixelSize * 1/3);
		var pos3:Number	= (_pixelSize * 2/3);
		
		// Build Phosphor Rectangles
		red 			= new Rectangle(pos1, 0, _phosphorWidth, _phosphorHeight);
		green 			= new Rectangle(pos2, 0, _phosphorWidth, _phosphorHeight);
		blue 			= new Rectangle(pos3, 0, _phosphorWidth, _phosphorHeight);
		
		// Build Overall Pixel
		drawPixel();
	}
	
	
	private function drawPixel():void {
		
		graphics.drawRect(0, 0, _pixelSize, _pixelSize);
		
		visible = false;
	}
	
}
}


